package com.xkdx.guangguang.fragment.my.setup;

import java.util.HashMap;
import java.util.List;

import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnCreateContextMenuListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.xkdx.caipiao.R;
import com.xkdx.guangguang.ManagerActivity;
import com.xkdx.guangguang.fragment.BaseFragment;
import com.xkdx.guangguang.fragment.brand.BrandInfoFragment;
import com.xkdx.guangguang.fragment.brand.branch.BranchFragment;
import com.xkdx.guangguang.fragment.shopinfo.ShopInfoFragment;
import com.xkdx.guangguang.fragment.user.UserSharePrefence1;
import com.xkdx.guangguang.module.network.AbsModule;
import com.xkdx.guangguang.module.statics.IConstants;
import com.xkdx.guangguang.module.util.ErrorModule;
import com.xkdx.guangguang.presistence.attention.DelBrandAction;
import com.xkdx.guangguang.presistence.attention.DelBrandModule;
import com.xkdx.guangguang.presistence.attention.DelBrandPresistence;
import com.xkdx.guangguang.presistence.attention.DelShopAction;
import com.xkdx.guangguang.presistence.attention.DelShopModule;
import com.xkdx.guangguang.presistence.attention.DelShopPresistence;
import com.xkdx.guangguang.presistence.attention.GetAttentionAction;
import com.xkdx.guangguang.presistence.attention.GetAttentionModule;
import com.xkdx.guangguang.presistence.attention.GetAttentionPresistence;
import com.xkdx.guangguang.shareclass.BrandDetail;
import com.xkdx.guangguang.shareclass.Shop;
import com.xkdx.guangguang.util.AsyncImageLoader;

public class AttentionFragment extends BaseFragment {

	public static String TAG = "AttentionFragment";
	View view;
	Button back;
	List<Shop> shopList;
	List<BrandDetail> brandList;
	ListView listView;
	TextView tab1, tab2;
	int choose = 1;
	int selectPosition;
	boolean start = false;
	MyAdapter adapter;
	GetAttentionAction getAttentionAction;
	GetAttentionModule getAttentionModule;
	GetAttentionPresistence getAttentionPresistence;
	ImageLoader imageLoader = ImageLoader.getInstance();

	// 删除商场关注
	DelShopAction delAction;
	DelShopModule delModule;
	DelShopPresistence delPresistence;
	// 删除品牌关注
	DelBrandAction delBrandAction;
	DelBrandModule delBrandModule;
	DelBrandPresistence delBrandPresistence;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.attention, container, false);
		findView();
		setOnClick();
		return view;
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		selectPosition = ((AdapterContextMenuInfo) item.getMenuInfo()).position;
		// Log.i(TAG, "selectPosition-->" + selectPosition);
		if (choose == 1) {
			delShopAttention();

		}
		if (choose == 2) {
			delBrandAttention();
		}

		return super.onContextItemSelected(item);
	}

	private void choose(int choose) {
		switch (choose) {
		case 1:
			tab1.setBackgroundResource(R.drawable.shopinfo_subview_title_click);
			tab2.setBackgroundResource(0);
			if (shopList != null) {
				adapter = new MyAdapter(true);
				listView.setAdapter(adapter);
			}
			break;
		case 2:
			tab2.setBackgroundResource(R.drawable.shopinfo_subview_title_click);
			tab1.setBackgroundResource(0);
			if (brandList != null) {
				adapter = new MyAdapter(false);
				listView.setAdapter(adapter);
			}
		default:
			break;
		}
	}

	class MyAdapter extends BaseAdapter {
		LayoutInflater inflater;
		AsyncImageLoader asyncImageLoader;
		boolean tag;

		public MyAdapter(boolean tag) {
			this.tag = tag;
			inflater = LayoutInflater.from(getActivity());
			asyncImageLoader = new AsyncImageLoader(getActivity());

		}

		@Override
		public int getCount() {
			if (tag) {
				return shopList.size();
			} else {
				return brandList.size();
			}

		}

		@Override
		public Object getItem(int position) {
			if (tag) {
				return shopList.get(position);
			} else {
				return brandList.get(position);
			}

		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.brand_search_item, null);
			}
			TextView tv = (TextView) convertView.findViewById(R.id.brand_search_item_brandname);
			final ImageView im = (ImageView) convertView.findViewById(R.id.brand_search_item_im);
			if (tag) {
				tv.setText(shopList.get(position).getShopName());
				imageLoader.displayImage(shopList.get(position).getLogo(), im);
				// Log.i(TAG, shopList.get(position).getLogo());
			} else {
				tv.setText(brandList.get(position).getBrandName());
				imageLoader.displayImage(brandList.get(position).getLogo(), im);
				// Log.i(TAG, brandList.get(position).getLogo());

			}

			return convertView;
		}

	}

	@Override
	public void findView() {
		back = (Button) view.findViewById(R.id.attention_back);
		listView = (ListView) view.findViewById(R.id.attentionList);
		tab1 = (TextView) view.findViewById(R.id.attenton_shop_tab1);
		tab2 = (TextView) view.findViewById(R.id.attenton_shop_tab2);
		if (shopList != null && brandList != null) {
			choose(choose);
		} else {
			showLoading();
		}

	}

	@Override
	public void setOnClick() {
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				getActivity().finish();
			}
		});
		tab1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				choose = 1;
				choose(choose);

			}
		});
		tab2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				choose = 2;
				choose(choose);
			}
		});

		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
				if (choose == 1) {
					FragmentTransaction ft = getFragmentManager().beginTransaction();
					String type=shopList.get(position).getShopType();
					if(type.equals("1")){
						ShopInfoFragment shopInfoFragment = new ShopInfoFragment();
						Bundle b = new Bundle();
						// Log.i(TAG, "shop-->" + position);
						b.putString("shopID", shopList.get(position).getShopID());
						shopInfoFragment.setArguments(b);
						ft.replace(R.id.fragment, shopInfoFragment);
						ft.addToBackStack(null);
						ft.commit();
					}else if(type.equals("2")){
						BranchFragment branchFragment = new BranchFragment();
						Bundle b = new Bundle();
						// Log.i(TAG, "shop-->" + position);
						b.putString("shopID", shopList.get(position).getShopID());
						branchFragment.setArguments(b);
						ft.replace(R.id.fragment, branchFragment);
						ft.addToBackStack(null);
						ft.commit();
					}
					
					
				}
				if (choose == 2) {
					// Log.i(TAG, "brandList-->" + position);
					FragmentTransaction ft = getFragmentManager().beginTransaction();
					BrandInfoFragment brandShopFragment = new BrandInfoFragment();
					Bundle b = new Bundle();
					b.putString("brandID", String.valueOf(brandList.get(position).getBrandID()));
					brandShopFragment.setArguments(b);
					ft.replace(R.id.fragment, brandShopFragment);
					ft.addToBackStack(null);
					ft.commit();
				}

			}
		});

		listView.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {

			@Override
			public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
				menu.add(0, 0, 0, "删除");
			}
		});

	}

	@Override
	public void showLoading() {

//		SharePrefenceUtil su = new SharePrefenceUtil(getActivity(), IConstants.SP_USER);
		UserSharePrefence1 su1 = new UserSharePrefence1(getActivity(),IConstants.SP_USER_NEW);
		getAttentionAction = new GetAttentionAction(su1.getId());
		getAttentionModule = new GetAttentionModule();
		getAttentionPresistence = new GetAttentionPresistence(this);
		getAttentionPresistence.setActitons(getAttentionAction);
		getAttentionPresistence.setModule(getAttentionModule);
		((ManagerActivity) getActivity()).addCurrentTask(getAttentionPresistence);
		getAttentionPresistence.execute();
		dataLoadDialog.setOnCancelListener(new OnCancelListener() {

			@Override
			public void onCancel(DialogInterface dialog) {
				getAttentionPresistence.cancel(true);
				getFragmentManager().popBackStack();

			}
		});
		dataLoadDialog.show();

	}

	@Override
	public void showLoaded(HashMap<String, AbsModule> map) {
		dataLoadDialog.dismiss();
		if (map.containsKey("0")) {
			shopList = getAttentionModule.shopList;
			brandList = getAttentionModule.brandList;
			choose(choose);
		} else {
			Toast.makeText(getActivity(), ((ErrorModule) map.get("1")).errorMessage, Toast.LENGTH_SHORT).show();
		}

	}

	private void delShopAttention() {

//		SharePrefenceUtil su = new SharePrefenceUtil(getActivity(), IConstants.SP_USER);
		UserSharePrefence1 su1 = new UserSharePrefence1(getActivity(),IConstants.SP_USER_NEW);
		delAction = new DelShopAction(su1.getId(), shopList.get(selectPosition).getShopID());
		delModule = new DelShopModule();
		delPresistence = new DelShopPresistence(this);
		((ManagerActivity) getActivity()).addCurrentTask(delPresistence);
		delPresistence.setActitons(delAction);
		delPresistence.setModule(delModule);
		delPresistence.execute();
		dataLoadDialog.setOnCancelListener(new OnCancelListener() {

			@Override
			public void onCancel(DialogInterface dialog) {
				if (delPresistence != null) {
					delPresistence.cancel(true);
				}

			}
		});
		dataLoadDialog.setMessage("正在删除...");
		dataLoadDialog.show();
	}

	private void delBrandAttention() {

//		SharePrefenceUtil su = new SharePrefenceUtil(getActivity(), IConstants.SP_USER);
		UserSharePrefence1 su1 = new UserSharePrefence1(getActivity(),IConstants.SP_USER_NEW);
		delBrandAction = new DelBrandAction(su1.getId(), String.valueOf(brandList.get(selectPosition).getBrandID()));
		delBrandModule = new DelBrandModule();
		delBrandPresistence = new DelBrandPresistence(this);
		((ManagerActivity) getActivity()).addCurrentTask(delPresistence);
		delBrandPresistence.setActitons(delBrandAction);
		delBrandPresistence.setModule(delBrandModule);
		delBrandPresistence.execute();
		dataLoadDialog.setOnCancelListener(new OnCancelListener() {

			@Override
			public void onCancel(DialogInterface dialog) {
				if (delBrandPresistence != null) {
					delBrandPresistence.cancel(true);
				}
			}
		});
		dataLoadDialog.setMessage("正在删除...");
		dataLoadDialog.show();
	}

	// 删除关注
	@Override
	public void showClick(HashMap<String, AbsModule> map) {
		dataLoadDialog.dismiss();
		if (map.containsKey("0")) {
			if (delModule.isDelSuccess) {
				shopList.remove(selectPosition);
				adapter.notifyDataSetChanged();
			} else {
				Toast.makeText(getActivity(), "删除失败", Toast.LENGTH_SHORT).show();
			}
		} else {
			Toast.makeText(getActivity(), ((ErrorModule) map.get("1")).errorMessage, Toast.LENGTH_SHORT).show();
		}
	}

	// 删除品牌关注
	@Override
	public void showLoadMore(HashMap<String, AbsModule> map) {
		dataLoadDialog.dismiss();
		if (map.containsKey("0")) {
			if (delBrandModule.isDelSuccess) {
				brandList.remove(selectPosition);
				adapter.notifyDataSetChanged();
			} else {
				Toast.makeText(getActivity(), "删除失败", Toast.LENGTH_SHORT).show();
			}
		} else {
			Toast.makeText(getActivity(), ((ErrorModule) map.get("1")).errorMessage, Toast.LENGTH_SHORT).show();
		}
	}

}
